package com.yangwei.examination.teacher.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.yangwei.examination.teacher.pojo.School;
import com.yangwei.examination.teacher.service.HandleClassService;
import com.yangwei.examination.teacher.service.SchoolService;
import com.yangwei.examination.teacher.pojo.HandleClass;
import com.yangwei.examination.user.pojo.Student;
import com.yangwei.examination.user.service.StudentService;
import com.yangwei.examination.utils.ResultUtils;
import com.yangwei.examination.utils.ResultVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Api(tags = "教师编辑班级",description = "管理班级")
@RestController
@RequestMapping("/tea/HandleClass")
public class HandleClassController {

    @Resource
    private HandleClassService handleClassService;

    @Resource
    private SchoolService schoolService;

    @Resource
    private StudentService studentService;

    //获取已经接入的班级
    @ApiOperation("获取已经接入的班级")
    @GetMapping("/list")
    public ResultVo getHandleClassList(@ApiParam(name = "id", value = "老师id唯一")@RequestParam Integer id) {

        QueryWrapper<HandleClass> handleQuery = new QueryWrapper<>();
        handleQuery.lambda().eq(HandleClass::getTeacherId, id).orderByDesc(HandleClass::getId);
        List<HandleClass> handleList = handleClassService.list(handleQuery);
        for (HandleClass handleClass : handleList) {
            //获取学校信息
            QueryWrapper<School> schoolQuery = new QueryWrapper<>();
            schoolQuery.lambda().eq(School::getSchoolId, handleClass.getSchoolId());
            School school = schoolService.getOne(schoolQuery);
            handleClass.setSchool(school);
            //获取每个班级的考生信息
            QueryWrapper<Student> studentQuery = new QueryWrapper<>();
            studentQuery.lambda()
                    .eq(Student::getClassNum, handleClass.getClassNum())
                    .eq(Student::getMajorName, handleClass.getMajorName())
                    .eq(Student::getSchoolId, handleClass.getSchoolId())
                    .eq(Student::getYear, handleClass.getYear());
            List<Student> studentList = studentService.list(studentQuery);
            handleClass.setStudentList(studentList);
        }

        return ResultUtils.success("查询成功", handleList);
    }

    //批量删除
    @ApiOperation("批量删除接入的班级 不包括考试信息和批改信息!")
    @PostMapping("/deleteByIds")
    public ResultVo delHandleClassByIds(@RequestBody Integer[] ids) {
        QueryWrapper<HandleClass> handleQuery = new QueryWrapper<>();
        handleQuery.lambda().in(HandleClass::getId, ids);
        boolean flag = handleClassService.remove(handleQuery);
        if (flag) {
            return ResultUtils.success("删除成功", null);
        }
        return ResultUtils.error("删除失败", null);
    }

    //单个删除
    @ApiOperation("单个删除 不包括考试信息和批改信息!")
    @GetMapping("/deleteById")
    public ResultVo deleteById(@RequestParam Integer id) {
        boolean flag = handleClassService.removeById(id);
        if (flag) {
            return ResultUtils.success("删除成功", null);
        }
        return ResultUtils.error("删除失败", null);
    }

    //编辑并唤起隐藏框
    @ApiOperation("点击编辑时数据回显")
    @GetMapping("/findById")
    public ResultVo findById(@RequestParam Integer id) {

        HandleClass handleClass = handleClassService.getById(id);

        QueryWrapper<School> handleQuery = new QueryWrapper<>();
        handleQuery.lambda().in(School::getSchoolId, handleClass.getSchoolId());
        School school = schoolService.getOne(handleQuery);
        handleClass.setSchool(school);

        if (handleClass.getSchoolId() == null) {
            return ResultUtils.error("查询失败", null);
        }
        return ResultUtils.success("查询成功", handleClass);
    }

    //编辑
    @ApiOperation("编辑班级信息")
    @PostMapping("/updateById")
    public ResultVo updateById(@RequestBody HandleClass handleClass) {
        handleClass.setSchoolId(handleClass.getSchool().getSchoolId());
        boolean flag = handleClassService.updateById(handleClass);

        if (flag) {
            return ResultUtils.success("编辑成功", null);

        }
        return ResultUtils.error("编辑失败", null);
    }

    //接入班级
    @ApiOperation("接入(新增)班级信息")
    @PostMapping("/save")
    public ResultVo save(@RequestBody HandleClass handleClass) {
        System.out.println(handleClass);
        handleClass.setSchoolId(handleClass.getSchool().getSchoolId());
        boolean save = handleClassService.save(handleClass);
        if (save) {
            return ResultUtils.success("添加成功", null);
        }
        return ResultUtils.error("添加失败", null);
    }

    //‘我管理的班级’ 用于教师主页统计
    @ApiOperation("‘我管理的班级’ 用于教师主页统计")
    @GetMapping("/getMyHandleClassCount")
    public ResultVo getMyHandleClassCount(@RequestParam("teacherId") Long teacherId) {
        int count = handleClassService.count(Wrappers.lambdaQuery(HandleClass.class)
                .eq(HandleClass::getTeacherId, teacherId)
        );
        return ResultUtils.success("查询成功", count);
    }

}
